#include "CheckFilePath.h"

#include <filesystem>
#include <fstream>

#include <utils/Print.h>

using namespace solar;

auto CheckFilePath::isTheDirectoryValid(const std::string& dir_path) -> bool
{
    bool ret = true;
    if (std::filesystem::exists(dir_path) && std::filesystem::is_directory(dir_path))
    {
        solar::println("WARNING::CheckFilePath::isTheDirectoryValid::invalid directory path: " +
                       dir_path);
        ret = false;
    }
    return ret;
}

auto CheckFilePath::isTheFileValid(const std::string& file_path) -> bool
{
    std::ifstream file_stream(file_path);
    if (!file_stream.good())
    {
        solar::println("WARNING::CheckFilePath::isTheDirectoryValid::invalid directory path: " +
                       file_path);
        return false;
    }
    return true;
}

void CheckFilePath::invertUrlToFilePath(std::string& str)
{
    int n = 0;
    auto len = str.size();
    for (int i = 1; i < len; i++)
    {
        if (str[i - 1] == '/' && str[i] != '/')
        {
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
            n = i;
#else
            n = i - 1;
#endif
            break;
        }
    }
    str.erase(str.begin(), str.begin() + n);
}
